﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKGeometryFuncs;

namespace YJKPatternSign.StairsRoomPlanSign.ModelLayer
{
	public class SameLevelStairsPacker
	{
		public List<KeyValuePair<Stairs, RevitLinkInstance>> LstStairs
		{
			get
			{
				return this.m_lstStairs;
			}
			private set
			{
				this.m_lstStairs = value;
			}
		}

		public double StairsTopValue
		{
			get
			{
				return this.m_dStairsTopValue;
			}
			private set
			{
				this.m_dStairsTopValue = value;
			}
		}

		public double StairsBaseValue
		{
			get
			{
				return this.m_dStairsBaseValue;
			}
			private set
			{
				this.m_dStairsBaseValue = value;
			}
		}

		public SameLevelStairsPacker(Stairs inputStairs, RevitLinkInstance inputLinkInstance = null)
		{
			this.LstStairs.Add(new KeyValuePair<Stairs, RevitLinkInstance>(inputStairs, inputLinkInstance));
			BoundingBoxXYZ boundingBoxXYZ = inputStairs.get_BoundingBox(null);
			XYZ xyz = boundingBoxXYZ.Max;
			XYZ xyz2 = boundingBoxXYZ.Min;
			Transform transform = null;
			if (inputLinkInstance != null)
			{
				transform = inputLinkInstance.GetTransform();
			}
			xyz = Geometry.TransformPoint(xyz, transform);
			xyz2 = Geometry.TransformPoint(xyz2, transform);
			this.StairsTopValue = xyz.Z;
			this.StairsBaseValue = xyz2.Z;
		}

		public void ApeendPacker(SameLevelStairsPacker input)
		{
			if (Geometry.LessThan(this.StairsTopValue, input.StairsTopValue))
			{
				this.StairsTopValue = input.StairsTopValue;
			}
			if (Geometry.LessThan(input.StairsBaseValue, this.StairsBaseValue))
			{
				this.StairsBaseValue = input.StairsBaseValue;
			}
			this.LstStairs.AddRange(input.LstStairs);
		}

		public List<KeyValuePair<StairsRun, RevitLinkInstance>> GetLstStairsRunWithRevitLinkInstance()
		{
			List<KeyValuePair<StairsRun, RevitLinkInstance>> list = new List<KeyValuePair<StairsRun, RevitLinkInstance>>();
			foreach (KeyValuePair<Stairs, RevitLinkInstance> keyValuePair in this.LstStairs)
			{
				foreach (ElementId elementId in keyValuePair.Key.GetStairsRuns())
				{
					StairsRun stairsRun = keyValuePair.Key.Document.GetElement(elementId) as StairsRun;
					if (stairsRun != null)
					{
						list.Add(new KeyValuePair<StairsRun, RevitLinkInstance>(stairsRun, keyValuePair.Value));
					}
				}
			}
			return list;
		}

		public List<KeyValuePair<StairsLanding, RevitLinkInstance>> GetLstStairsLandingWithRevitLinkInstance()
		{
			List<KeyValuePair<StairsLanding, RevitLinkInstance>> list = new List<KeyValuePair<StairsLanding, RevitLinkInstance>>();
			foreach (KeyValuePair<Stairs, RevitLinkInstance> keyValuePair in this.LstStairs)
			{
				foreach (ElementId elementId in keyValuePair.Key.GetStairsLandings())
				{
					StairsLanding stairsLanding = keyValuePair.Key.Document.GetElement(elementId) as StairsLanding;
					if (stairsLanding != null)
					{
						list.Add(new KeyValuePair<StairsLanding, RevitLinkInstance>(stairsLanding, keyValuePair.Value));
					}
				}
			}
			return list;
		}

		public double GetStepsHeight()
		{
			double num = 0.0;
			foreach (KeyValuePair<Stairs, RevitLinkInstance> keyValuePair in this.m_lstStairs)
			{
				num = Math.Max(keyValuePair.Key.ActualRiserHeight, num);
			}
			return num;
		}

		private List<KeyValuePair<Stairs, RevitLinkInstance>> m_lstStairs = new List<KeyValuePair<Stairs, RevitLinkInstance>>();

		private double m_dStairsTopValue;

		private double m_dStairsBaseValue;
	}
}
